//
//  SISSHouseTakeLookDetailViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/8/31.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSHouseTakeLookDetailViewController.h"
#import "SISSNetworkingManager+Progress.h"
#import "SISSNetworkingManager+HouseProgress.h"
#import "SISSHouseProgressModel.h"
#import "SISSPassengerInfoView.h"
#import "SISSHouseTakeLookModel.h"
#import "SISSHouseTakeLookDetailModel.h"
#import "SISSCommentModel.h"

@interface SISSHouseTakeLookDetailViewController ()

/* 客源信息View */
@property (weak, nonatomic) SISSPassengerInfoView *passengerInfoView;
/* 带看详情模型 */
@property (strong, nonatomic) SISSHouseTakeLookDetailModel *takeLookDetailModel;


@end

@implementation SISSHouseTakeLookDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)setTakeLookDetailModel:(SISSHouseTakeLookDetailModel *)takeLookDetailModel {
    _takeLookDetailModel = takeLookDetailModel;
    
    NSMutableArray *imagesURLStrings = [NSMutableArray arrayWithCapacity:takeLookDetailModel.takeLookPic.count];
    for (SISSTakeLookPicModel *model in takeLookDetailModel.takeLookPic) {
        [imagesURLStrings addObject:model.picPath];
    }
    
    self.cycleScrollView.imageURLStringsGroup = imagesURLStrings;
    self.imagesURLStrings = imagesURLStrings.copy;
    NSInteger index = takeLookDetailModel.takeLookPic.count > 0 ? 1 : 0;
    self.pageLabel.text = [NSString stringWithFormat:@"%zd/%zd",index,self.imagesURLStrings.count];
    
    
    self.passengerInfoView.takeLookDetailModel = takeLookDetailModel;
}


- (void)setHouseProgressModel:(SISSHouseProgressModel *)houseProgressModel {
    _houseProgressModel = houseProgressModel;
    self.brokerState = self.takeLookModel.buyerState == 1 && self.takeLookModel.sellerState == 1;
    SISSPassengerInfoView *passengerInfoView = [[SISSPassengerInfoView alloc] init];
    self.passengerInfoView = passengerInfoView;
    self.infoView = passengerInfoView;
}

- (void)setTakeLookModel:(SISSHouseTakeLookModel *)takeLookModel {
    _takeLookModel = takeLookModel;
    self.brokerState = takeLookModel.buyerState == 1 && takeLookModel.sellerState == 1;
}

- (void)bottomButtonAssinment {
    
    self.bottomButton.hidden = NO;
    self.trueButton.hidden = YES;
    self.untrueButton.hidden = YES;
    
    NSString *buttonStr;
    if (self.takeLookModel.takelookType == 1) { // 线上
        if (self.takeLookModel.sellerState == 0 && self.takeLookModel.buyerState == 0) {
            buttonStr = @"等待确认";
        } else if (self.takeLookModel.sellerState == 2 || self.takeLookModel.buyerState == 2) {
            buttonStr = self.takeLookModel.sellerState == 2 ? @"卖家否认看房" : @"买家否认看房";
        } else if (self.takeLookModel.sellerState == 1 && self.takeLookModel.buyerState == 1) {
            buttonStr = @"评价";
        } else if (self.takeLookModel.sellerState == 1 || self.takeLookModel.buyerState == 1) {
            buttonStr = self.takeLookModel.sellerState == 1 ? @"等待买家确认" : @"等待卖家确认";
        }
    } else if (self.takeLookModel.takelookType == 2) { // 线下
        if (self.takeLookModel.sellerState == 0) {
            buttonStr = @"等待确认";
        } else if (self.takeLookModel.sellerState == 2) {
            buttonStr = @"对方否认看房";
        } else if (self.takeLookModel.sellerState == 1) {
            buttonStr = @"评价";
        }
    }

    [self.bottomButton setTitle:buttonStr forState:UIControlStateNormal];
}

/**
 删除带看
 */
- (void)deleteTakeLook {
    
    [kNetworkManager deleteHouseTakelookWithId:self.takeLookModel.ID type:self.takeLookModel.takelookType success:^(id responseObject) {
        
        if (self.deleteCompletedBlock) {
            self.deleteCompletedBlock();
        }
        [self.navigationController popViewControllerAnimated:YES];
        
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}


/**
 加载评论数据
 */
- (void)requestCommentData {
    
    WeakSelf
    [kNetworkManager requestCommentTakeLookSellerCommentBrokerInfoWithAtlrId:self.takeLookModel.ID success:^(NSArray<SISSCommentModel *> *commentArr) {
        
        if (commentArr.count == 0) {
            weakSelf.beCommentedView.hidden = YES;
        } else {
            weakSelf.beCommentedView.hidden = NO;
            weakSelf.beAcrId = commentArr.firstObject.ID;
            weakSelf.beCommentedView.commentModel = commentArr.firstObject;
        }
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
    [kNetworkManager requestCommentTakeLookBrokerCommentSellerInfoWithAtlrId:self.takeLookModel.ID takelookType:self.takeLookModel.takelookType success:^(NSArray<SISSCommentModel *> *commentArr) {
        
        if (commentArr.count == 0) {
            weakSelf.toCommentShowView.hidden = YES;
        } else {
            weakSelf.toCommentShowView.hidden = NO;
            SISSCommentModel *commentModel = commentArr.firstObject;
            weakSelf.toCommentShowView.commentModel = commentModel;
            NSString *buttonStr;
            if (commentModel.lstOfAdditional.firstObject) {
                buttonStr = @"已评价";
                weakSelf.bottomButton.userInteractionEnabled = NO;
            } else {
                weakSelf.toAcrId = commentModel.ID;
                weakSelf.isAdditionalComment = YES;
                buttonStr = @"追加";
            }
            [self.bottomButton setTitle:buttonStr forState:UIControlStateNormal];
        }
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

/**
 带看详情
 */
- (void)requestTakeLookDetailData {
    
    WeakSelf
    [kNetworkManager requestHouseTakelookDetailWithId:self.takeLookModel.ID type:self.takeLookModel.takelookType success:^(SISSHouseTakeLookDetailModel *takeLookDetailModel) {
        
        weakSelf.takeLookDetailModel = takeLookDetailModel;
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

/**
 评论
 */
- (void)addComment {
    
    WeakSelf
//    NSString *content = [NSString unicodeToISO88591:self.midView.commentTextView.text];
    NSString *content = self.midView.commentTextView.text;
    if ([NSString stringContainsEmoji:content]) {
        [self alertActionAlertWithTitle:@"提示" message:@"暂不支持表情等特殊字符评价。"];
        return;
    }
    NSDictionary *parameter = @{
                                @"atlrId":self.takeLookModel.ID,
                                @"ahbpId":self.houseProgressModel.ahbpId,
                                @"score":@(self.midView.gradeStar),
                                @"content":content,
                                @"takelook_type":@(self.takeLookModel.takelookType),
                                @"comment_type":@(1)
                                };
    
    [kNetworkManager insertBrokerCommentSellerWithParameters:parameter success:^(id responseObject) {
        
        [SISSTools showSVWithTitle:@"评论成功"];
        [weakSelf requestCommentData];
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

/**
 追加
 */
- (void)addAdditionalComment {
    NSLog(@"%@",self.additonalTextView.text);
    WeakSelf
    //    NSString *content = [NSString unicodeToISO88591:self.additonalTextView.text];
    NSString *content = self.additonalTextView.text;
    if ([NSString stringContainsEmoji:content]) {
        [self alertActionAlertWithTitle:@"提示" message:@"暂不支持表情等特殊字符评价。"];
        return;
    }
    [kNetworkManager insertAdditionalCommentWithAcrId:self.toAcrId content:content commentType:1 success:^(id responseObject) {
        
        [SISSTools showSVWithTitle:@"评论成功"];
        [weakSelf requestCommentData];
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
